package com.heima.wemedia.service.impl;

import com.github.pagehelper.PageHelper;
import com.heima.file.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmMaterialDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.dao.WmMaterialDao;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 17:12:52
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {

    @Autowired
    private MinIOTemplate minIOTemplate;

    @Resource
    private WmMaterialDao wmMaterialDao;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }

    @Override
    public org.springframework.data.domain.Page<WmMaterial> queryByPage(WmMaterial wmMaterial, PageRequest pageRequest) {
        return null;
    }


    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(wmMaterial.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmMaterialDao.deleteById(id) > 0;
    }

    @Override
    public ResponseResult uploadPic(MultipartFile multipartFile) throws IOException {
            //判断参数是否为空
            if (multipartFile==null&&multipartFile.getSize() == 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            //获取图片名
            String originalFilename = multipartFile.getOriginalFilename();
            //获取随机文件名
            String fileName = UUID.randomUUID().toString().replace("-", "");
            //获取文件后缀名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //将随机文件名与文件后缀名拼接
            fileName=fileName+suffix;
            //保存图片，返回图片路径
            String url = minIOTemplate.uploadImgFile("", fileName, multipartFile.getInputStream());
            //保存数据到数据库
            WmMaterial wmMaterial = new WmMaterial();
            System.out.println(WmThreadLocalUtil.getUser() == null);
            wmMaterial.setUserId(Long.valueOf(WmThreadLocalUtil.getUser().getId()));
            wmMaterial.setUrl(url);
            wmMaterial.setType(0);
            wmMaterial.setIsCollection(0);
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setUpdatedTime(new Date());
            wmMaterialDao.insert(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        WmMaterial wmMaterial=new WmMaterial();
        wmMaterial.setUserId(Long.valueOf(WmThreadLocalUtil.getUser().getId()));
        if (dto.getIsCollection()==1) {
            wmMaterial.setIsCollection(Integer.valueOf(dto.getIsCollection()));
        }
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmMaterial> page = wmMaterialDao.queryAll(wmMaterial);
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),(int) page.getTotal()) ;
        result.setData(page.getResult());

        return result;
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial wmMaterial = wmMaterialDao.queryById(id.longValue());
        if (wmMaterial == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        try {
            wmMaterialDao.deleteById(id.longValue());
        }catch (Exception e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult collect(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial wmMaterial=new WmMaterial();
        wmMaterial.setId(id.longValue());
        wmMaterial.setIsCollection(1);
        wmMaterialDao.updateCollectionById(wmMaterial);
        return ResponseResult.setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult cancelCollect(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial wmMaterial=new WmMaterial();
        wmMaterial.setId(id.longValue());
        wmMaterial.setIsCollection(0);
        wmMaterialDao.updateCollectionById(wmMaterial);
        return ResponseResult.setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS);
    }
}
